﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.Services;
using AstoriaPlus;
using System.Net;
using System.ServiceModel;
using System.Threading;

namespace AstoriaPlus
{
	public abstract class EntityInterceptor<TDataService, TDataSource, TEntity> : IEntityInterceptor<TDataService,TDataSource,TEntity>
		where TDataService : DataService<TDataSource>
		where TDataSource : class
		where TEntity : class
	{
		public TDataService Service { get; set; }
		public TDataSource DataSource { get; set; }
		object IEntityInterceptor<TDataService, TDataSource>.Entity { get; set; }
		public UpdateOperations UpdateOperations { get; set; }

		public TEntity Entity
		{
			get { return (TEntity)((IEntityInterceptor<TDataService, TDataSource>)this).Entity; }
			set { ((IEntityInterceptor<TDataService, TDataSource>)this).Entity = value; }
		}

		protected virtual void OnExecute()
		{
			switch (this.UpdateOperations)
			{
				case UpdateOperations.Add:
					this.OnAdd();
					break;
				case UpdateOperations.Change:
					this.OnChange();
					break;
				case UpdateOperations.Delete:
					this.OnDelete();
					break;
			}
		}

		protected virtual void OnAdd() { }
		protected virtual void OnChange() { }
		protected virtual void OnDelete() { }

		void IEntityInterceptor<TDataService, TDataSource>.Execute()
		{
			if (((IEntityInterceptor<TDataService, TDataSource>)this).Entity is TEntity)
			{
				this.OnExecute();
			}
		}
	}
}